package com.unisv.dj;

import com.unisv.dj.R.id;
import com.unisv.dj.util.Command;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * <p>
 * init activity
 * </p>
 * 
 * @author fanzhen
 * @time 8:04:13 PM Sep 23, 2011
 * @email fanzhen.meng@gmail.com
 */
public class HeartRateActivity extends Activity {

	// private static final String TAG = "AutoActivity";
	BroadcastReceiver commandReceiver;
	IntentFilter movementFilter;
	TextView tvheartts;
	ImageView imgBack;
	ImageView imgAgain;
	Button btnBack;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.heartrate);
		tvheartts = (TextView) findViewById(id.tvheartts);
		imgBack = (ImageView) findViewById(id.imgBack);
		imgAgain = (ImageView) findViewById(id.imgAgain);
		btnBack = (Button) findViewById(id.btnBack);

		btnBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		imgAgain.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// Music
				Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
				startActivity(intent);
			}
		});

		imgBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// Vodeo
				Intent intent = new Intent();
				intent.setAction(android.content.Intent.ACTION_VIEW);
				intent.setDataAndType(Uri.EMPTY, "video/*");
				startActivity(intent);
			}
		});
	}

	@Override
	public void onResume() {
		if (movementFilter == null)
			movementFilter = new IntentFilter(ServerService.COMMAND_COMING);
		if (commandReceiver == null)
			commandReceiver = new BroadcastReceiver() {
				@Override
				public void onReceive(Context context, Intent intent) {
					// Log.i(TAG, "msg from service.............." + command);
					byte[] comm = intent.getByteArrayExtra(ServerService.COMMAND_TAG);
					if (comm.length == 4 && comm[0] == 110 && comm[1] == 2) {
						tvheartts.setText(String.format("%1d", comm[2]));
					}
				}
			};
		registerReceiver(commandReceiver, movementFilter);
		// Command.regHeartRate();
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		// Command.unregHeartRate();
	}

	@Override
	protected void onDestroy() {
		if (commandReceiver != null) {
			unregisterReceiver(commandReceiver);
			commandReceiver = null;
		}
		if (movementFilter != null)
			movementFilter = null;
		super.onDestroy();
	}

}
